package com.example.demo.controller;

import com.example.demo.common.UpDownServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.service.UserServiceImpl;
import com.example.demo.vo.MyAgentVo;
import com.example.demo.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/18 0018.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UpDownServiceImpl upDownServiceImpl;



    //根据用户增加下级会员信息,返回json
    @RequestMapping(value = "/addAgent",method = RequestMethod.POST)
    @ResponseBody
    public List<User> addAgentByUserName(@RequestBody User user,@RequestParam("leaderName")String  leaderName, Map map){
        //根据用户创建下级会员代理
        User leader=userService.findByProperty(leaderName);
        user.setParentId(leader.getId());
        userService.save(user);
        List<User> userList=userService.findAgentByParentId(leader.getId());
        map.put("userList",userList);
        return userList;
    }

    //删除商品信息,重定向到查询所有商品,返回json
    @RequestMapping(value = "/delUser",method = RequestMethod.POST)
    public String  delUser(int id){
        userService.del((long)id);
        return "redirect:/user/findUser";
    }

    //根据用户删除下级会员信息,返回json
    @RequestMapping(value = "/delAgent",method = RequestMethod.POST)
    @ResponseBody
    public List<User> addAgentByUserName(@RequestParam("userId")String  userId, Map map){
        User user=userService.findById(Long.parseLong(userId));
        long parentId=user.getParentId();
        userService.del(Long.parseLong(userId));
        List<User> userList=userService.findAgentByParentId(parentId);
        map.put("userList",userList);
        return userList;
    }

    //修改用户信息
    @RequestMapping(value = "/updateUser")
    @ResponseBody
    public User modifyUser(@RequestBody User u){
        System.out.println("UserController u.getId()==="+u.getId());
        userService.update(u);
        User user = userService.findByProperty(u.getName());
        return user;
    }

    //修改下级会员代理信息
    @RequestMapping(value = "/updateAgent",method = RequestMethod.POST)
    public List<User> modifyMyAgent(@RequestBody User  userAgent,Map map){
        System.out.println("UserController p.getId()==="+userAgent.getId());
        System.out.println("UserController p.getUserId()==="+userAgent.getParentId());
        userService.update(userAgent);
        List<User> userList=userService.findAgentByParentId(userAgent.getParentId());
        map.put("userList",userList);
        return userList;
    }

    //查询所有用户信息
    @RequestMapping(value = "/findUser")
    public String findUser(){
        return "user";
    }

    //查询所有用户信息,返回json
    @RequestMapping(value = "/findUserJson")
    @ResponseBody
    public List<User> findUserJson(Map map){
        List<User> userList=userService.findAll();

        map.put("userList",userList);
        for (int i=0;i<userList.size();i++) {
            System.out.println("--"+userList.get(i).getName());
            System.out.println("--"+userList.get(i).getPhone());
            System.out.println("---"+userList.get(i).getDescription());
            System.out.println("--"+userList.get(i).getAddress());
            System.out.println("---"+userList.get(i).getCompany());
            System.out.println("----------------"+userList.get(i).getId());
        }
        return userList;
    }

    //根据用户查询上级会员信息,返回json
    @RequestMapping(value = "/findLeader",method = RequestMethod.POST)
    @ResponseBody
    public User findLeaderByName(@RequestParam("name") String name, Map map){
        User myUser=userService.findByProperty(name);
        User user=userService.findLeaderByParentIdAsId(myUser.getParentId());
        map.put("user",user);
        return user;
    }

    //根据用户查询下级会员信息,返回json
    @RequestMapping(value = "/findAgent",method = RequestMethod.POST)
    @ResponseBody
    public List<User> findAgentByName(@RequestParam("name") String name, Map map){
        //根据用户查询是否存在，不存在则创建用户
        User user=userService.findByProperty(name);
        List<User> userList=userService.findAgentByParentId(user.getId());
        map.put("userList",userList);
        return userList;
    }

    //根据用户昵称查询会员信息,返回json
    @RequestMapping(value = "/findUserByName",method = RequestMethod.POST)
    @ResponseBody
    public User findByName(@RequestParam("name") String name, Map map){
        System.out.println(" userController findByName---name=-----"+name);
        User user=userService.findByProperty(name);
        if(user==null){
            logger.info("-----用户不存在----");
            User newUser=new User();
            newUser.setName(name);
            userService.save(newUser);
             newUser=userService.findByProperty(name);
            return newUser;
        }else {
            map.put("user", user);
            return user;
        }
    }

    //处理文件上传
    @RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
    @ResponseBody
    /*public  String uploadImg(@RequestParam("file") MultipartFile file,
                             HttpServletRequest request) {*/
        public Map<String, Object> headImgUpload(HttpServletRequest request,@RequestParam("file")MultipartFile file) {
            Map<String, Object> value = new HashMap<String, Object>();
            value.put("success", true);
            value.put("errorCode", 0);
            value.put("errorMsg", "");
            try {
                String head = upDownServiceImpl.updateHead(file, 4);//此处是调用上传服务接口，4是需要更新的userId 测试数据。
                value.put("data", head);
            } catch (IOException e) {
                e.printStackTrace();
                value.put("success", false);
                value.put("errorCode", 200);
                value.put("errorMsg", "图片上传失败");
            } catch (Exception e) {
                e.printStackTrace();
            }
        return value;
    }
}
